package com.cjd.common.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.cjd.common.R
import kotlinx.android.synthetic.main.base_app_bar_layout.*


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2020-03-05
 * description 公共 design toolbar
 */
abstract class BaseDesignActivity : BaseActivity() {
    protected val mTvToolbarTitle: TextView? by lazy {
        getRootView()?.findViewById<TextView>(R.id.base_tv_toolbar_title)
    }

    protected val mIvToolbarBack: ImageView? by lazy {
        getRootView()?.findViewById<ImageView>(R.id.base_iv_toolbar_back).apply {
            getToolbarBackIcon()?.let {
                if (it > 0)
                    this?.setImageResource(it)
            }
        }
    }

    protected val mIvToolbarNext: ImageView? by lazy {
        getRootView()?.findViewById<ImageView>(R.id.base_iv_toolbar_next)
    }

    protected val mTvToolbarNext: TextView? by lazy {
        getRootView()?.findViewById<TextView>(R.id.base_tv_toolbar_next)
    }

    protected val mTvToolbarBack: TextView? by lazy {
        getRootView()?.findViewById<TextView>(R.id.base_tv_toolbar_back)
    }
    private val toolbarView: View by lazy {
        layoutInflater.inflate(R.layout.base_replace_toolbar, null, false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initToolbar()
    }

    /**
     * 获取 自定义 toolbar root
     */
    fun getRootView(): View? {
        if (replaceToolbar())
            return toolbarView
        return null
    }

    /**
     * 初始化toolbar
     */
    private fun initToolbar() {
        setSupportActionBar(base_toolbar)
        if (replaceToolbar()) {
            supportActionBar?.let {
                it.setDisplayShowCustomEnabled(true);
                it.setDisplayShowHomeEnabled(false);
                it.setBackgroundDrawable(null);
                val layoutParams: Toolbar.LayoutParams = Toolbar.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                it.setCustomView(toolbarView, layoutParams)
                mIvToolbarBack?.let {
                    it.setOnClickListener {
                        finish()
                    }
                }
                mTvToolbarTitle?.let { tv ->
                    getToolbarTitle()?.let {
                        tv.text = it
                    }
                }
            }
        }
    }

    /**
     * replaceToolbar() = true 时有效
     * @return String
     */
    protected open fun getToolbarTitle(): String? = null

    /**
     * 替换原始 toolbar
     * @return Boolean
     */
    protected open fun replaceToolbar(): Boolean = true

    /**
     * 返回图标替换
     * @return drawable 资源id
     */
    protected open fun getToolbarBackIcon(): Int? = null

}